package Test;

import java.util.*;

public class Demo6 {
    public static void main(String[] args) {
        Random r=new Random();
        loop:for (int i = 0; i < 10000; i++) {
            StringBuilder sb=new StringBuilder();
            int len = r.nextInt(1, 500);
            for (int j = 0; j < len; j++) {
                sb.append((char)('a'+r.nextInt(26)));
            }
            String ste=sb.toString();
            String[] a1=method1(ste);
            String[] a2=method2(ste);

            if(a1.length!=a2.length){
                System.out.println("结果错误");
                System.out.println(ste);
                System.out.println("-------------------------------------------------------");
                for (String s : a1) {
                    System.out.println(s);
                }
                System.out.println("-------------------------------------------------------");
                for (String s : a2) {
                    System.out.println(s);
                }
                break loop;
            }else{
                boolean t=true;
                for (int l = 0; l < a1.length; l++) {
                    if(!a1[l].equals(a2[l])){
                        System.out.println(ste);
                        t=false;
                        System.out.println("结果错误");
                        for (String s : a1) {
                            System.out.println(s);
                        }
                        System.out.println("-------------------------------------------------------");
                        for (String s : a2) {
                            System.out.println(s);
                        }
                        break loop;
                    }
                }
                if(t){
                    System.out.println("对的");
                }
            }

        }

    }
    public static String[] method1(String str){
        int len = str.length();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            for (int j = i + 2; j <=len; j++) {
                if (new StringBuilder(str.substring(i, j)).reverse().toString().equals(str.substring(i, j))) {
                    list.add(str.substring(i, j));
                }
            }
        }
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.length()-o2.length();
            }
        });
        String[] a = list.toArray(new String[list.size()]);
      return a;
    }
    public static String[] method2(String str){
        int len = str.length();//获取字符串长度
        int cnt = 2;//回文子串长度至少为2
        ArrayList<String> list=new ArrayList<>();
        while(cnt<=len) {//注意要包括它自己
            for (int j = 0; j+cnt <= str.length(); j++) {
                if(new StringBuffer(str.substring(j,j+cnt)).reverse().toString().equals( str.substring(j, j+cnt))) list.add(str.substring(j, j+cnt));//从左往右输出和从右往左输出结果是一样的字符串
            }
            cnt++;
        }
        return list.toArray(new String[list.size()]);
    }
}
